package represent.type;

public class StringType implements Type {

	private String val;

	public StringType(String s) {
		val = s;
	}

	public String asJavaVal() {
		return val;
	}

	public boolean equals(Object obj) {
		StringType sc = null;
		if(obj instanceof DateType)
			sc = new StringType( (String) ((DateType)obj).asJavaVal() );
		else if(obj instanceof StringType)
			sc = (StringType) obj;
		else 
			throw new Error("Inconsistent types.");
		return val.equals(sc.val);
	}

	public int hashCode() {
		return val.hashCode();
	}

	public String toString() {
		return val;
	}

	public Type divide(Type c) {
		throw new Error("Undefined operation.");
	}

	public String getType() {
		return "char";
	}

	public Type minus(Type c) {
		throw new Error("Undefined operation.");
	}

	public Type mod(Type c) {
		throw new Error("Undefined operation.");
	}

	public Type mult(Type c) {
		throw new Error("Undefined operation.");
	}

	public Type plus(Type c) {
		if(! (c instanceof StringType))
			throw new Error("Unconsistent types.");
		return new StringType(val + (String) (c.asJavaVal()));
	}

	public int compareTo(Type c) {
		StringType sc = null;
		if(c instanceof DateType)
			sc = new StringType( (String) ((DateType)c).asJavaVal() );
		else if(c instanceof StringType)
			sc = (StringType) c;
		else 
			throw new Error("Inconsistent types.");
		return val.compareTo(sc.val);
	}

	public static int compare(byte[] a, byte[] b) {
		// TODO �Զ���ɷ������
		return 0;
	}

	public static StringType fromBytes(byte[] bytes) {
		// TODO �Զ���ɷ������
		return null;
	}

	public int TypeToInt() {
		return utils.Constant.STRING;
	}
	
	public static int length() {
		return 4;
	}
	
}
